package com.numberone.master.common.encryption;

import java.util.Map;

import com.numberone.master.common.utils.BASE64;

public class RSATester {

    static String publicKey;
    static String privateKey;

    static {
        try {
            Map<String, Object> keyMap = RSAUtils.genKeyPair();
            publicKey = RSAUtils.getPublicKey(keyMap);
            privateKey = RSAUtils.getPrivateKey(keyMap);
            System.out.println("公钥: \n\r" + publicKey);
            System.out.println("私钥： \n\r" + privateKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) throws Exception {
    	testHttpSign();
    }
    
    static void testHttpSign() throws Exception {
        String param = "http://fanyi.baidu.com/translate?aldtype=16047&query=&keyfrom=baidu&smartresult=dict&lang=auto2zh#zh/en/%E4%BB%A3%E7%A0%81&query=&keyfrom=baidu&smartresult=dict&lang=哈哈哈哈哈哈哈哈哈哈我的呢是什么啊";
        byte[] encodedData = RSAUtils.encryptByPrivateKey(param.getBytes(), privateKey);
        String str = BASE64.encodeBASE64(encodedData);
        System.out.println("加密后十六进制：" + str);
        
        byte[] decodedData = RSAUtils.decryptByPublicKey(BASE64.decodeBASE64(str), publicKey);
        System.out.println("解密后：" + new String(decodedData));
        
        String sign = RSAUtils.sign(BASE64.decodeBASE64(str), privateKey);
        System.out.println("签名：" + sign);
        
        boolean status = RSAUtils.verify(BASE64.decodeBASE64(str), publicKey, sign);
        System.out.println("签名验证结果：" + status);
    }
    
}